﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;

namespace YArchitech.Revit
{
	public class HYSystemElementFilter
	{
		public HYSystemElementFilter(Document document)
		{
			this.doc = document;
		}

		public FilteredElementCollector GetActiveViewElemSet()
		{
			return new FilteredElementCollector(this.doc, this.doc.ActiveView.Id);
		}

		public FilteredElementCollector GetActiveViewElemSet(View view)
		{
			return new FilteredElementCollector(this.doc, view.Id);
		}

		public FilteredElementCollector GetAllElementSet()
		{
			ElementClassFilter item = new ElementClassFilter(typeof(FamilyInstance));
			ElementClassFilter item2 = new ElementClassFilter(typeof(HostObject));
			List<ElementFilter> list = new List<ElementFilter>();
			ElementCategoryFilter item3 = new ElementCategoryFilter(BuiltInCategory.OST_Stairs);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_StairsRailing);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_Ramps);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_RoomSeparationLines);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaceSeparationLines);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_Lines);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel);
			((ICollection<ElementFilter>)list).Add(item3);
			item3 = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
			((ICollection<ElementFilter>)list).Add(item3);
			((ICollection<ElementFilter>)list).Add(item);
			((ICollection<ElementFilter>)list).Add(item2);
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(list);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.WherePasses(logicalOrFilter);
			return filteredElementCollector;
		}

		public FilteredElementCollector GetElementsByBICategory(BuiltInCategory bICategory)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfCategory(bICategory);
			return filteredElementCollector;
		}

		private IList<BuiltInCategory> GetBuiltInCategorySet(IList<string> listStr)
		{
			IList<BuiltInCategory> list = new List<BuiltInCategory>();
			if (listStr == null || listStr.Count <= 0)
			{
				return list;
			}
			foreach (string text in listStr)
			{
				uint num = <PrivateImplementationDetails>.ComputeStringHash(text);
				if (num <= 1575471522u)
				{
					if (num <= 528386081u)
					{
						if (num != 210525008u)
						{
							if (num != 527117667u)
							{
								if (num == 528386081u)
								{
									if (text == "PipeSchedules")
									{
										list.Add(-2008105);
									}
								}
							}
							else if (text == "PipeSegments")
							{
								if (UnifiedModified.GetPipeSegments() != -1)
								{
									list.Add(UnifiedModified.GetPipeSegments());
								}
							}
						}
						else if (text == "水管附件")
						{
							list.Add(-2008055);
						}
					}
					else if (num != 610928660u)
					{
						if (num != 1535291702u)
						{
							if (num == 1575471522u)
							{
								if (text == "材料")
								{
									list.Add(-2000700);
								}
							}
						}
						else if (text == "风口")
						{
							list.Add(-2008013);
						}
					}
					else if (text == "机械设备")
					{
						list.Add(-2001140);
					}
				}
				else if (num <= 2573028048u)
				{
					if (num != 1645839806u)
					{
						if (num != 1821648329u)
						{
							if (num == 2573028048u)
							{
								if (text == "风管")
								{
									list.Add(-2008000);
								}
							}
						}
						else if (text == "水管管件")
						{
							list.Add(-2008049);
						}
					}
					else if (text == "风管附件")
					{
						list.Add(-2008016);
					}
				}
				else if (num <= 3367301222u)
				{
					if (num != 2874052367u)
					{
						if (num == 3367301222u)
						{
							if (text == "水管")
							{
								list.Add(-2008044);
							}
						}
					}
					else if (text == "风管管件")
					{
						list.Add(-2008010);
					}
				}
				else if (num != 3914730892u)
				{
					if (num == 4121427430u)
					{
						if (text == "喷头")
						{
							list.Add(-2008099);
						}
					}
				}
				else if (text == "卫生设备")
				{
					list.Add(-2001160);
				}
			}
			return list;
		}

		public FilteredElementCollector GetFilterByCondition(IList<string> listStr, bool visible, View view)
		{
			FilteredElementCollector filteredElementCollector = null;
			if (visible)
			{
				if (view != null)
				{
					filteredElementCollector = this.GetActiveViewElemSet(view);
				}
				else
				{
					filteredElementCollector = this.GetActiveViewElemSet();
				}
			}
			else
			{
				filteredElementCollector = this.GetAllElementSet();
			}
			if (listStr == null || listStr.Count <= 0)
			{
				return filteredElementCollector;
			}
			IList<BuiltInCategory> builtInCategorySet = this.GetBuiltInCategorySet(listStr);
			if (builtInCategorySet.Count <= 0)
			{
				return filteredElementCollector;
			}
			IList<ElementFilter> list = new List<ElementFilter>();
			foreach (BuiltInCategory builtInCategory in builtInCategorySet)
			{
				ElementCategoryFilter item = new ElementCategoryFilter(builtInCategory);
				list.Add(item);
			}
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(list);
			return filteredElementCollector.WherePasses(logicalOrFilter);
		}

		public ElementSet GetElemSetByParamValue(BuiltInParameter biParam, string paramValue)
		{
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = this.GetFilterByCondition(null, false, null).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					Parameter parameter = element.get_Parameter(biParam);
					if (parameter != null && !(parameter.AsString() != paramValue))
					{
						elementSet.Insert(element);
					}
				}
			}
			return elementSet;
		}

		public ElementSet GetElemSetByParamValue(string paramName, string paramValue)
		{
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = this.GetFilterByCondition(null, false, null).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					Parameter parameter = element.GetParameter(paramName);
					if (parameter != null && !(parameter.AsString() != paramValue))
					{
						elementSet.Insert(element);
					}
				}
			}
			return elementSet;
		}

		public IList<PipeType> GetPipeTypes()
		{
			IList<PipeType> list = new List<PipeType>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfClass(typeof(PipeType));
			IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
			enumerator.Reset();
			while (enumerator.MoveNext())
			{
				Element element = enumerator.Current;
				PipeType pipeType = element as PipeType;
				if (pipeType != null)
				{
					list.Add(pipeType);
				}
			}
			return list;
		}

		public IList<Element> GetAllDuctFitting()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_DuctFitting);
			return filteredElementCollector.ToElements();
		}

		public Element GetNewDuctFitting(IList<Element> listElem1, IList<Element> listElem2)
		{
			Element result = null;
			foreach (Element element in listElem2)
			{
				bool flag = false;
				IEnumerator<Element> enumerator2 = listElem1.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Id == element.Id)
						{
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					result = element;
					break;
				}
			}
			return result;
		}

		private Document doc;
	}
}
